package com.yx.shop.dao.admin.impl;

import com.yx.shop.dao.TypeDao;
import com.yx.shop.dao.admin.AdminTypeDao;
import com.yx.shop.entitly.Type;
import com.yx.shop.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Classname TypeDaoImpl
 * @Description TODO:
 * @Date 2021/05/07 11:49
 * @Created by yx
 */
public class AdminTypeDaoImpl implements AdminTypeDao {
    private QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
    @Override
    public List<Type> getAllType() {
        String sql = "SELECT * FROM TYPE;";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Type.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer insertType(Type type) {
        try {
            String sql = "insert into type(tname,tinfo) values(?,?);";
            return queryRunner.update(sql,type.getTname(),type.getTinfo());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer deleteType(Integer tid) {
        try {
            String sql = "delete from type where tid= ?;";
            return queryRunner.update(sql,tid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
